package com.beehoo.dcportal.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.BusinessBean;
import com.beehoo.dcportal.service.IBusinessServ;
import com.beehoo.dcportal.utils.CommonUtils;

public class BusinessAction extends BaseAction {

	private static final long serialVersionUID = 3785604778744387141L;
	private IBusinessServ businessServ;
	private List<BusinessBean> businessLs;
	private String condition;
	private int totalRows;                   //列表条目总数
	private int pageRows;                    //每页条目数
	private int pageNo;                      //当前页码
	private String action;                   //请求地址

	public int getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}

	public int getPageRows() {
		return pageRows;
	}

	public void setPageRows(int pageRows) {
		this.pageRows = pageRows;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public List<BusinessBean> getBusinessLs() {
		return businessLs;
	}

	public void setBusinessLs(List<BusinessBean> businessLs) {
		this.businessLs = businessLs;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public void setBusinessServ(IBusinessServ businessServ) {
		this.businessServ = businessServ;
	}

	public String execute(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		String queryStreet = "";
		condition = CommonUtils.nvlToString(condition);
		if(condition.equals("")){
			queryStreet = streetId;
		}else{
			try {
				condition = URLDecoder.decode(condition, "utf-8");
			} catch (UnsupportedEncodingException e) {
				setMessage(super.getText("custom.messages.error.business.query.condition.empty"));
			}
		}
		pageRows = Constant.MSG_DEFAULT_PAGEROWS;
		if(pageNo == 0)
			pageNo = 1;
		try {
			businessLs = businessServ.queryBusiness(queryStreet, condition, pageNo, pageRows);
			totalRows = businessServ.queryBusinessCount(queryStreet, condition);
			if (condition.equals("")) {
				action = "/business/";
			} else {
				action = "/business/" + URLEncoder.encode(condition, "utf-8") + "/";
			}
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		} catch (UnsupportedEncodingException e) {
			setMessage(super.getText("custom.messages.error.business.query.error"));
			
		}
		return SUCCESS;
	}

}
